const {
  DataTypes
} = require('sequelize');
const DateUtils = require('../utils/dateUtils');

module.exports = sequelize => {
  const attributes = {
    id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: null,
      comment: "主键",
      primaryKey: true,
      field: "id",
      autoIncrement: true
    },
    module: {
      type: DataTypes.CHAR,
      allowNull: false,
      defaultValue: null,
      comment: "日志模块",
      primaryKey: false,
      field: "module",
      autoIncrement: false
    },
    requestMethod: {
      type: DataTypes.CHAR,
      allowNull: false,
      defaultValue: null,
      comment: "请求方式",
      primaryKey: false,
      field: "request_method",
      autoIncrement: false
    },
    requestParams: {
      type: DataTypes.TEXT,
      allowNull: true,
      defaultValue: null,
      comment: "请求参数(批量请求参数可能会超过text)",
      primaryKey: false,
      field: "request_params",
      autoIncrement: false
    },
    responseContent: {
      type: DataTypes.TEXT,
      allowNull: true,
      defaultValue: null,
      comment: "返回参数",
      primaryKey: false,
      field: "response_content",
      autoIncrement: false
    },
    content: {
      type: DataTypes.CHAR,
      allowNull: false,
      defaultValue: null,
      comment: "日志内容",
      primaryKey: false,
      field: "content",
      autoIncrement: false
    },
    requestUri: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "请求路径",
      primaryKey: false,
      field: "request_uri",
      autoIncrement: false
    },
    method: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "方法名",
      primaryKey: false,
      field: "method",
      autoIncrement: false
    },
    ip: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "IP地址",
      primaryKey: false,
      field: "ip",
      autoIncrement: false
    },
    province: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "省份",
      primaryKey: false,
      field: "province",
      autoIncrement: false
    },
    city: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "城市",
      primaryKey: false,
      field: "city",
      autoIncrement: false
    },
    executionTime: {
      type: DataTypes.BIGINT,
      allowNull: true,
      defaultValue: null,
      comment: "执行时间(ms)",
      primaryKey: false,
      field: "execution_time",
      autoIncrement: false
    },
    browser: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "浏览器",
      primaryKey: false,
      field: "browser",
      autoIncrement: false
    },
    browserVersion: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "浏览器版本",
      primaryKey: false,
      field: "browser_version",
      autoIncrement: false
    },
    os: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "终端系统",
      primaryKey: false,
      field: "os",
      autoIncrement: false
    },
    createBy: {
      type: DataTypes.BIGINT,
      allowNull: true,
      defaultValue: null,
      comment: "创建人ID",
      primaryKey: false,
      field: "create_by",
      autoIncrement: false
    },
    createTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "创建时间",
      primaryKey: false,
      field: "create_time",
      autoIncrement: false,
      get() {
        const rawValue = this.getDataValue('createTime');
        return rawValue ? DateUtils.formatDate(rawValue, 'YYYY-MM-DD HH:mm:ss') : null;
      }
    },
    isDeleted: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: null,
      comment: "逻辑删除标识(1-已删除 0-未删除)",
      primaryKey: false,
      field: "is_deleted",
      autoIncrement: false
    }
  };
  const options = {
    tableName: "sys_log",
    comment: "",
    indexes: [{
      name: "idx_create_time",
      unique: false,
      fields: ["create_time"]
    }]
  };
  const SysLogModel = sequelize.define('sys_log_model', attributes, options);

  SysLogModel.associate = function(models) {
    // 添加与用户表的关联
    SysLogModel.belongsTo(models.sys_user_model, {
      foreignKey: 'createBy',
      targetKey: 'id',
      as: 'creator'
    });
  };

  return SysLogModel;
};